package com.clinic.dao.imp;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.clinic.dao.interfaces.Doctor_dao;
import com.clinic.domain.Doctor;
import com.clinic.util.Util_imp;

public class Doctor_imp implements Doctor_dao {
	SessionFactory session_factory = null;

	public Doctor_imp() {
		session_factory = Util_imp.get_sessionfactory();
	}

	@Override
	public int create_doctor(Doctor c) {
		System.out.println("Enter - create_doctor");
		Session session = session_factory.openSession();
		Transaction tx = null;
		int dr_obj_id = 0;
		try {
			tx = session.beginTransaction();
			dr_obj_id = (Integer) session.save(c);
			tx.commit();
			System.out.println("New Doctor Created with ID: " + dr_obj_id);
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Create Doctor Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - create_doctor");
		return dr_obj_id;
	}

	@Override
	public void update_doctor(Doctor dr) {
		System.out.println("Enter - update_doctor");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(dr);
			tx.commit();
			System.out.println("Doctor Object Updated");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Doctor Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - update_doctor");
	}

	@Override
	public void delete_doctor(int dr_obj_id) {
		System.out.println("Enter - delete_doctor");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Doctor d = (Doctor) session.get(Doctor.class, dr_obj_id);
			session.delete(d);
			tx.commit();
			System.out.println("Doctor with ID: " + dr_obj_id + " Deleted");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Doctor Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - delete_doctor");
	}

	@Override
	public Doctor get_doctor(int dr_obj_id) {
		System.out.println("Enter - get_doctor");
		Session session = session_factory.openSession();
		Transaction tx = null;
		Doctor d = null;
		try {
			tx = session.beginTransaction();
			d = (Doctor) session.createQuery("from Doctor where dr_obj_id=:id")
					.setParameter("id", dr_obj_id).uniqueResult();
			tx.commit();
			System.out.println("Returned Object Name: " + d.getFull_name());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_doctor");
		return d;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Doctor> get_all_doctors() {
		System.out.println("Enter - get_all_doctors");
		Session session = session_factory.openSession();
		List<Doctor> l = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			l = session.createQuery("from Doctor").list();
			tx.commit();
			System.out.println("Number of Doctors returned is: " + l.size());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_all_doctors");
		return l;
	}

	@Override
	public Doctor persistOrMerge(Doctor d) {
		return (Doctor) session_factory.getCurrentSession().merge(d);
	}
	
	@Override
	public int getsize(){
		System.out.println("Enter - getsize");
		Session session = session_factory.openSession();
		int size = 0;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			size = ((Long) session.createQuery("select count(*) from Doctor").uniqueResult()).intValue();
			tx.commit();
			System.out.println("Count(*) Doctor: " + size);
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - getsize");
		return size;
	}
}